package com.yanqu.road.dao.impl.activity.lottery;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.lottery.ActivityLotteryInfo;
import com.yanqu.road.entity.activity.lottery.ActivityLotteryParamInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityLotteryConfigDaoImpl extends TempDao {

    private static String getActivityLotteryInfoSql = "select * from `t_s_activity_lottery` where `activityId`=? ORDER BY `id` ASC";
    public Map<Integer,List<ActivityLotteryInfo>> getActivityLotteryInfoList(int activityId) {
        Map<Integer,List<ActivityLotteryInfo>> listMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getActivityLotteryInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityLotteryInfo activityLotteryInfo = new ActivityLotteryInfo();
                activityLotteryInfo.setActivityId(rs.getInt("activityId"));
                activityLotteryInfo.setId(rs.getInt("id"));
                activityLotteryInfo.setName(rs.getString("name"));
                activityLotteryInfo.setWeights(rs.getInt("weights"));
                activityLotteryInfo.setRewards(rs.getString("rewards"));
                activityLotteryInfo.setIsHide(rs.getInt("isHide"));
                activityLotteryInfo.setSort(rs.getInt("sort"));
                activityLotteryInfo.setIsNotice(rs.getInt("isNotice"));
                activityLotteryInfo.setShowWeights(rs.getInt("showWeights"));
                activityLotteryInfo.setLotteryType(rs.getInt("lotteryType"));
                if(!listMap.containsKey(activityLotteryInfo.getLotteryType())){
                    listMap.put(activityLotteryInfo.getLotteryType(),new ArrayList<>());
                }
                listMap.get(activityLotteryInfo.getLotteryType()).add(activityLotteryInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return listMap;
    }

    private static String getActivityLotteryParamInfoSql = "select * from `t_s_activity_lottery_param` where `activityId`=? ORDER BY `id` ASC";
    public Map<Integer,ActivityLotteryParamInfo> getActivityLotteryParamInfoMap(int activityId) {
        Map<Integer,ActivityLotteryParamInfo> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getActivityLotteryParamInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityLotteryParamInfo dataInfo = new ActivityLotteryParamInfo();
                dataInfo.setActivityId(rs.getInt("activityId"));
                dataInfo.setId(rs.getInt("id"));
                dataInfo.setSkinExtractLuckyParam(rs.getString("skinExtractLuckyParam"));
                dataInfo.setSkinExtractAddParam(rs.getString("skinExtractAddParam"));
                dataInfo.setSkinExtractRewardRepeatControl(rs.getString("skinExtractRewardRepeatControl"));
                dataInfo.setSkinShowList(rs.getString("skinShowList"));
                dataInfo.setSkinExtractSpecialParam(rs.getString("skinExtractSpecialParam"));
                dataInfo.setLotteryType(rs.getInt("lotteryType"));
                dataInfo.setInterfaceStyle(rs.getInt("interfaceStyle"));
                dataInfo.setName(rs.getString("name"));
                dataInfo.setDesc(rs.getString("desc"));

                map.put(dataInfo.getLotteryType(),dataInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


}
